﻿class TextSelectionRange
{
    start: TextPosition;
    end: TextPosition;

    constructor()
    {
        this.start = new TextPosition(0, 0);
        this.end = new TextPosition(0, 0);
    }

    static withRange(range: TextSelectionRange): TextSelectionRange
    {
        var newRange: TextSelectionRange = new TextSelectionRange();
        newRange.start.character = range.start.character;
        newRange.start.line = range.start.line;
        newRange.end.character = range.end.character;
        newRange.end.line = range.end.line;
        return newRange;
    }

    static withPoints(start: TextPosition, end: TextPosition): TextSelectionRange
    {
        var range: TextSelectionRange = new TextSelectionRange();
        range.start.character = start.character;
        range.start.line = start.line;
        range.end.character = end.character;
        range.end.line = end.line;
        return range;
    }
    constainsPoint(point: TextPosition): boolean
    {
        if (this.isEmpty())
            return false;

        var start: TextPosition = this.start;
        var end: TextPosition = this.end;

        if (start.compare(end) === ComparisonResult.OrderedDescending)
        {
            start = this.end;
            end = this.start;
        }
        
        if ((start.line <= point.line) && (end.line >= point.line))
        {
            if (start.line === end.line)
                if ((start.character > point.character) || (end.character < point.character))
                    return false;
            if ((start.line === point.line) && (start.character > point.character))
                return false;
            if ((end.line === point.line) && (end.character < point.character))
                return false;

            return true;
        }
        return false;
    }
    isEmpty(): boolean
    {
        return ((this.start.line === this.end.line) && (this.start.character === this.end.character));
    }
}